<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Infotech Ltd.
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: mslaria $ ($Date: 2006/10/04 23:00:44 $)
// $RCSfile: admin_tasks.php,v $ : $Revision: 1.14 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================

if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}


if (!$staffauth->isAdmin()) {
	trigger_error("You do not have permission to access $PHP_SELF", E_USER_ERROR);
}

require_once ("./includes/functions_cron.php");
$cron = new Cron;

require_once ("./includes/functions_html.php");

$template->loadLanguageSection("tasks");


/**
* ###############################################
* FUNCTION DECLARATIONS
* ###############################################
*/

/**
* Callback for parsing cron logs data
*/
function _taskLogFields($arg)
{
	global $_SWIFT;
	$record = &$arg[0];
	$fields = &$arg[1];

	$record["cronlogs.crontitle"] = $record["crontitle"];
	$record["cronlogs.description"] = $record["description"];
	$record["cronlogs.dateline"] = edate($_SWIFT["settings"]["dt_datetimeformat"], $record["dateline"]);

	$record["options"] = '<a onClick="javascript:doConfirm(\''. $_SWIFT["language"]["actionconfirm"] .'\', \'index.php?_m=core&_a=tasklog&do=delete&cronlogid='. $record["cronlogid"] .'\');" href="#" title="'. $_SWIFT["language"]["delete"] .'"><img src="'. $_SWIFT["themepath"] .'icon_delete.gif" border="0">&nbsp;'. $_SWIFT["language"]["delete"] .'</a>&nbsp;';
	return $record;
}


/**
* Mass Action: Delete Task Logs
*/
function _maDeleteTaskLogs()
{
	global $_SWIFT;
	$cron = new Cron;

	if (!is_array($_POST["itemid"]) || $_SWIFT["isdemo"] == true)
	{
		return false;
	}

	$cron->deleteTaskLogs($_POST["itemid"]);
}



/**
* ###############################################
* Manage Tasks
* ###############################################
*/
if ($eventaction == "managetasks")
{

	/**
	* ###############################################
	* Run the task right now
	* ###############################################
	*/
	if ($_GET["do"] == "run" && trim($_GET["taskid"]) != "" && $_SWIFT["isdemo"] != true)
	{
		$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."cron` WHERE `cronid` = '". intval($_GET["taskid"]) ."';");
		$dbCore->nextRecord();
		if (trim($dbCore->Record["module"]) != "" && trim($dbCore->Record["action"]) != "")
		{
			$Record = $dbCore->Record;

			$dbCore->query("UPDATE `". TABLE_PREFIX ."cron` SET `nextrun` = '". (DATENOW-5) ."' WHERE `cronid` = '". $Record["cronid"] ."';");
			$settings->updateKey("cron", "nextrun", (DATENOW-5), true);
			$template->assign("runcron", true);
			$infomessage = sprintf($_SWIFT["language"]["scheduledexecute"], $_SWIFT["language"][$Record["name"]]);
		}
	
	/**
	* ###############################################
	* Disable the specified task
	* ###############################################
	*/
	} else if ($_GET["do"] == "disable" && trim($_GET["taskid"]) != "" && $_SWIFT["isdemo"] != true) {
		$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."cron` WHERE `cronid` = '". intval($_GET["taskid"]) ."';");
		$dbCore->nextRecord();

		if ($dbCore->Record["cronid"] != "")
		{
			$dbCore->query("UPDATE `". TABLE_PREFIX ."cron` SET `autorun` = '0' WHERE `cronid` = '". $dbCore->escape($_GET["taskid"]) ."';", 2);

			$infomessage = sprintf($_SWIFT["language"]["taskdisabled"], $_SWIFT["language"][$dbCore->Record["name"]]);
		}
	
	/**
	* ###############################################
	* Enable the specified task
	* ###############################################
	*/
	} else if ($_GET["do"] == "enable" && trim($_GET["taskid"]) != "" && $_SWIFT["isdemo"] != true) {
		$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."cron` WHERE `cronid` = '". intval($_GET["taskid"]) ."';");
		$dbCore->nextRecord();

		if ($dbCore->Record["cronid"] != "")
		{
			$dbCore->query("UPDATE `". TABLE_PREFIX ."cron` SET `autorun` = '1' WHERE `cronid` = '". intval($_GET["taskid"]) ."';", 2);

			$infomessage = sprintf($_SWIFT["language"]["taskenabled"], $_SWIFT["language"][$dbCore->Record["name"]]);			
		}
	}
	
	
	/**
	* ###############################################
	* Default display of task listing
	* ###############################################
	*/
	$interface->adminHeader($_SWIFT["language"]["scheduledtasks"]." > ".$_SWIFT["language"]["manage"]);

	$interface->adminNavBar($_SWIFT["language"]["scheduledtasks"], $_SWIFT["language"]["desc_scheduledtasks"], 10);

	printInfoBox($infomessage);

	printMainTableHeader($_SWIFT["language"]["tasklist"]);

	$colarray[0]["align"] = "left";
	$colarray[0]["nowrap"] = true;
	$colarray[0]["value"] = $_SWIFT["language"]["title"];
	$colarray[1]["width"] = "200";
	$colarray[1]["value"] = $_SWIFT["language"]["nextrun"];
	$colarray[2]["width"] = "200";
	$colarray[2]["value"] = $_SWIFT["language"]["lastrun"];
	$colarray[3]["width"] = "150";
	$colarray[3]["value"] = $_SWIFT["language"]["runsevery"];
	$colarray[4]["width"] = "150";
	$colarray[4]["value"] = $_SWIFT["language"]["options"];
	
	printDataRow($colarray, "tabletitlerow");


	$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."cron` ORDER BY `cronid` ASC;");
	while ($dbCore->nextRecord())
	{
		$colarray = array();
		$colarray[0]["align"] = "left";

		if (empty($_SWIFT["language"][$dbCore->Record["name"]]))
		{
			$name = $dbCore->Record["name"];
		} else {
			$name = $_SWIFT["language"][$dbCore->Record["name"]];
		}

		if ($dbCore->Record["autorun"] == "1")
		{
			$colarray[0]["value"] = $name;
		} else {
			$colarray[0]["value"] = '<span class="disabledtext">'.$name."</span>";
		}

		$colarray[1]["value"] = formattedDate($dbCore->Record["nextrun"], true);
		$colarray[2]["value"] = formattedDate($dbCore->Record["lastrun"], true);

		$Record = &$dbCore->Record;
		if ($Record["cminute"] == -1 && $Record["cday"] == 0 && $Record["chour"] == 0)
		{
			// Append one minute to this and save
			$everyx = sprintf($_SWIFT["language"]["xminute"], "1");
		} else if ($Record["cminute"] > 0 && $Record["cday"] == 0 && $Record["chour"] == 0) {
			// Append $cminute to this and save, this means we run it every x minutes
			$everyx = sprintf($_SWIFT["language"]["xminute"], $Record["cminute"]);
		} else if ($Record["chour"] == -1 && $Record["cday"] == 0 && $Record["cminute"] == 0) {
			// We run this every 1 hour
			$everyx = sprintf($_SWIFT["language"]["xhour"], "1");
		} else if ($Record["chour"] > 0 && $Record["cday"] == 0 && $Record["cminute"] == 0) {
			// We run this every x hour
			$everyx = sprintf($_SWIFT["language"]["xhour"], $Record["chour"]);
		} else if ($Record["cday"] == -1 && $Record["cminute"] == 0 && $Record["chour"] == 0) {
			// We run this every day
			$everyx = sprintf($_SWIFT["language"]["xday"], "1");
		} else if ($Record["cday"] > 0 && $Record["chour"] == 0 && $Record["cminute"] == 0) {
			// We run this every x days
			$everyx = sprintf($_SWIFT["language"]["xday"], $Record["cday"]);
		}

		$colarray[3]["value"] = $everyx;

		$iconlist = '<a href="index.php?_m=core&_a=managetasks&do=run&taskid='. urlencode($dbCore->Record["cronid"]) .'" title="'. $_SWIFT["language"]["runnow"] .'"><img src="'. $_SWIFT["themepath"] .'icon_run.gif" border="0">&nbsp;'.$_SWIFT["language"]["runnow"].'</a>';

		if ($dbCore->Record["autorun"] == 1)
		{
			$iconlist .= '&nbsp;&nbsp;<a href="index.php?_m=core&_a=managetasks&do=disable&taskid='. urlencode($dbCore->Record["cronid"]) .'" title="'. $_SWIFT["language"]["disable"] .'"><img src="'. $_SWIFT["themepath"] .'icon_disable.gif" border="0">&nbsp;'.$_SWIFT["language"]["disable"].'</a>';
		} else {
			$iconlist .= '&nbsp;&nbsp;<a href="index.php?_m=core&_a=managetasks&do=enable&taskid='. urlencode($dbCore->Record["cronid"]) .'" title="'. $_SWIFT["language"]["enable"] .'"><img src="'. $_SWIFT["themepath"] .'icon_enable.gif" border="0">&nbsp;'.$_SWIFT["language"]["enable"].'</a>';
		}

		$colarray[4]["value"] = $iconlist;

		printDataRow($colarray);
	}

	printMainTableFooter();

	$interface->adminFooter();


/**
* ###############################################
* Task Log
* ###############################################
*/
} else if ($eventaction == "tasklog") {
	if ($_GET["do"] == "delete" && $_GET["cronlogid"] != "")
	{
		$dbCore->query("SELECT `crontitle`, `cronlogid`, `cronid` FROM `". TABLE_PREFIX ."cronlogs` WHERE `cronlogid` = '". intval($_GET["cronlogid"]) ."';");
		$dbCore->nextRecord();

		if ($dbCore->Record["cronlogid"] == $_GET["cronlogid"] && trim($_GET["cronlogid"]) != "" && $_SWIFT["isdemo"] != true)
		{
			$infomessage = sprintf($_SWIFT["language"]["cronlogdeleteconfirm"], $dbCore->Record["crontitle"]);
			$cron->deleteTaskLogs(array($_GET["cronlogid"]));
		}
	}

	$options["recordsperpage"] = "6";
	$options["sortby"] = "cronlogs.dateline";
	$options["sortorder"] = "desc";
	$options["massaction"][0]["title"] = $_SWIFT["language"]["delete"];
	$options["massaction"][0]["callback"] = "_maDeleteTaskLogs";

	$options["idname"] = "cronlogid";
	$options["quicksearch"] = true;

	$options["advancedsearch"][0]["title"] = $_SWIFT["language"]["title"];
	$options["advancedsearch"][0]["query"] = 'SELECT * FROM `'. TABLE_PREFIX .'cronlogs` AS cronlogs LEFT JOIN `'. TABLE_PREFIX .'cron` AS cron ON (cron.cronid = cronlogs.cronid) WHERE cronlogs.title LIKE \'%$_searchstr%\' $_sortjoin;';
	$options["advancedsearch"][1]["title"] = $_SWIFT["language"]["status"];
	$options["advancedsearch"][1]["query"] = 'SELECT * FROM `'. TABLE_PREFIX .'cronlogs` AS cronlogs LEFT JOIN `'. TABLE_PREFIX .'cron` AS cron ON (cron.cronid = cronlogs.cronid) WHERE cronlogs.description LIKE \'%$_searchstr%\' $_sortjoin;';

	$options["callback"] = "_taskLogFields";

	$fields[0]["name"] = "cronlogs.crontitle";
	$fields[0]["title"] = $_SWIFT["language"]["title"];
	$fields[0]["width"] = "";


	$fields[1]["name"] = "cronlogs.description";
	$fields[1]["title"] = $_SWIFT["language"]["status"];
	$fields[1]["width"] = "200";
	$fields[1]["align"] = "center";

	$fields[2]["name"] = "cronlogs.dateline";
	$fields[2]["title"] = $_SWIFT["language"]["executiontime"];
	$fields[2]["width"] = "200";
	$fields[2]["align"] = "center";

	$fields[3]["type"] = "custom";
	$fields[3]["name"] = "options";
	$fields[3]["title"] = $_SWIFT["language"]["options"];
	$fields[3]["width"] = "80";
	$fields[3]["align"] = "center";

	$grid = new Grid();

	$interface->adminHeader($_SWIFT["language"]["scheduledtasks"]." > ".$_SWIFT["language"]["tasklog"], 1);

	$interface->adminNavBar($_SWIFT["language"]["scheduledtasks"], $_SWIFT["language"]["desc_scheduledtasks"] , 10);
	printInfoBox($infomessage);

	$grid->start("tasklog", $_SWIFT["language"]["tasklogs"], 'SELECT * FROM `'. TABLE_PREFIX .'cronlogs` AS cronlogs LEFT JOIN `'. TABLE_PREFIX .'cron` AS cron ON (cron.cronid = cronlogs.cronid) $_sortjoin;', 'SELECT COUNT(*) AS `totalitems` FROM `'. TABLE_PREFIX .'cronlogs` AS cronlogs LEFT JOIN `'. TABLE_PREFIX .'cron` AS cron ON (cron.cronid = cronlogs.cronid);', 'SELECT * FROM `'. TABLE_PREFIX .'cronlogs` AS cronlogs LEFT JOIN `'. TABLE_PREFIX .'cron` AS cron ON (cron.cronid = cronlogs.cronid) WHERE cronlogs.description LIKE \'%$_searchstr%\' $_sortjoin;', $fields, $options);
	$grid->display("tasklog");
	
	$interface->adminFooter();
}

?>